<?php

namespace App\Http\Model;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class FinanceAgentj extends Base
{
    protected $table = 'finance_agentj_stream';
    protected $agentJdLogTable = 'agent_jd_log';

    private $redis_cache_finance_user_wallet = "CACHE:FINANCE:USER:WALLET";

    public function __construct(array $attributes = [])
    {
        parent::__construct($this->table, $attributes);
    }


    /**
     * 更新用户代理金豆
     * @param int $userId 更新该user_id
     * @param int $agentjType 类型 1=钻石兑换获得 2=金币兑换获得 51=转出扣除 52=解除代理扣除
     * @param int $dataId 数据ID
     * @param int $dataType 数据类型 1=userId 2=钻石值 3=金币值
     * @param int $changeValue 变动值 支出使用负数
     * @param string $changeDesc 变动描述
     * @return bool
     */
    public function inc($userId, $dataId, $agentjType, $changeValue, $changeDesc){
        if(empty($userId)) {
            return false;
        }
        if(empty($changeValue)) {
            return false;
        }
        $dataType = 0;
        if(in_array($agentjType, [1])) {
            $dataType = 2;
        }elseif(in_array($agentjType, [2])) {
            $dataType = 3;
        }elseif(in_array($agentjType, [51])) {
            $dataType = 1;
        }

        if(in_array($agentjType, [1,2])) {
            $changeValue = abs($changeValue);
        }else{
            $changeValue = 0 - abs($changeValue);
        }


        DB::table("finance_wallet")
            ->where('user_id', '=', $userId)
            ->increment('agent_jd_balance', $changeValue, ['change_timestamp'=> time()]);

        DB::table($this->table)->insert([
            "user_id" => $userId,
            "change_value" => $changeValue,
            "change_desc" => $changeDesc,
            "agent_type" => $agentjType,
            "data_id" => $dataId,
            "data_type" => $dataType,
            "change_timestamp" => time()
        ]);

        Redis::DEL($this->redis_cache_finance_user_wallet . ':' . $userId);
        return true;
    }

    //添加代理金豆转入记录
    public function createAgentJdLog($data)
    {
        $data['auto_grant_at'] = time() + 3600;
        $data['agentd_type'] = 1;
        $data['created_at'] = $data['updated_at'] = date('Y-m-d H:i:s', time());
        return DB::table($this->agentJdLogTable)->insertGetId($data);
    }

    //更新代理金豆转入记录
    public function updateAgentJdLog($id, $data=[], $where=[])
    {
        $upData = [];
        if(isset($data['agentd_type'])) {
            $upData['agentd_type'] = $data['agentd_type'];
        }
        if(!empty($upData)) {
            DB::table($this->agentJdLogTable)
                ->where('id', $id)
                ->update($upData);
        }

        return 1;
    }

    //获取用户代理交易记录
    public function getAgentjStreamList($where, $page, $limit)
    {
        $query = DB::table($this->table)->orderBy('change_timestamp', 'desc')
            ->offset($page * $limit)
            ->limit($limit);
        if(!empty($where['user_id'])) {
            $query = $query->where('user_id', $where['user_id']);
        }
        if(!empty($where['agent_type'])) {
            $query = $query->whereIn('agent_type', $where['agent_type']);
        }
        if(!empty($where['time_st'])) {
            $query = $query->where('change_timestamp', '>=', $where['time_st']);
        }
        if(!empty($where['time_ed'])) {
            $query = $query->where('change_timestamp', '<=', $where['time_ed']);
        }

        $data = $query->get();
        $data = self::objToArray($data);

        return $data;
    }

    /**
     * 获取代理金豆转入记录
     */
    public function getAgentJdLog($id){
        $info = DB::table($this->agentJdLogTable)
            ->where("id", $id)
            ->first();
        $info = self::objToArray($info);

        return $info;
    }

    /**
     * 获取需要自动发放未领取的代理金豆转入记录
     */
    public function getAgentJdLogByType($time){
        $list = DB::table($this->agentJdLogTable)
            ->where("agentd_type", 1)
            ->where("auto_grant_at", '<=', $time)
            ->get();
        $list = self::objToArray($list);

        return $list;
    }



}
